package org.ptkparty.client;

import java.util.Set;

import org.ptkparty.client.mapper.AppPlaceHistoryMapper;
import org.ptkparty.client.mapper.PTKPartyActivityMapper;
import org.ptkparty.client.model.ChampionshipProxy;
import org.ptkparty.client.place.PlayerDto;
import org.ptkparty.client.place.ProfilPlace;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PTKParty implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final ClientFactory clientFactory = GWT.create(ClientFactory.class);

		VisualizationUtils.loadVisualizationApi(new Runnable() {

			@Override
			public void run() {
				clientFactory.getPtkService().logUser(new AsyncCallback<PlayerDto>() {

					@Override
					public void onFailure(Throwable caught) {

					}

					@Override
					public void onSuccess(final PlayerDto player) {
						Context.get().setCurrentPlayer(player);

						clientFactory.getFactory().championchipRequest().getAll().fire(new Receiver<Set<ChampionshipProxy>>() {

							@Override
							public void onSuccess(Set<ChampionshipProxy> response) {
								Context.get().setChampionshipList(response);

								SimplePanel appWidget = new SimplePanel();
								appWidget.setStyleName("");

								EventBus eventBus = clientFactory.getEventBus();
								PlaceController placeController = clientFactory.getPlaceController();

								ActivityMapper activityMapper = new PTKPartyActivityMapper(clientFactory);
								ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
								activityManager.setDisplay(appWidget);

								AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
								PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
								historyHandler.register(placeController, eventBus, new ProfilPlace((Context.get().getChampionshiplist() != null && !Context.get().getChampionshiplist().isEmpty()) ? Context.get().getChampionshiplist().iterator().next().getId() : "", player.getId()));

								RootPanel.get("loading").setVisible(false);
								RootPanel.get().add(appWidget);

								historyHandler.handleCurrentHistory();
							}
						});
					}
				});
			}
		}, LineChart.PACKAGE);
	}
}
